package com.lcj.dr.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.lcj.dr.controller.form.DriverSettingsForm;
import com.lcj.dr.db.dao.DriverDao;
import com.lcj.dr.db.dao.DriverSettingsDao;
import com.lcj.dr.service.DriverSettingsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Author : lcj
 * @create 2022/11/16 16:27
 */
@Service
public class DriverSettingsServiceImpl implements DriverSettingsService {
    @Resource
    private DriverSettingsDao settingsDao;

    @Resource
    private DriverDao driverDao;

    /**
     * 查询司机设置
     * @param driverId
     * @return
     */
    @Override
    public HashMap searchDriverSettings(long driverId) {
        //查询司机设置
        String settings = settingsDao.searchDriverSettings(driverId);
        HashMap map = JSONUtil.parseObj(settings).toBean(HashMap.class);
        boolean bool = MapUtil.getInt(map, "listenService") == 1 ? true : false;
        map.replace("listenService", bool);
        bool = MapUtil.getInt(map, "autoAccept") == 1 ? true : false;
        map.replace("autoAccept", bool);
        //查询司机家地址
        String driverHome = driverDao.searchDriverHome(driverId);
        map.put("home", driverHome);
        return map;
    }

    /**
     * 更新司机设置
     * @param form
     * @return
     */
    @Override
    public Long updateDriverSettings(DriverSettingsForm form) {
        Long rows = settingsDao.updateSettings(form);
        return rows;
    }
}
